package com.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.common.R;
import com.hospital.controller.FileController;
import com.hospital.dao.PatientDao;
import com.hospital.pojo.Patient;
import com.hospital.pojo.Picture;
import com.hospital.service.PatientService;
import com.hospital.service.PictureService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author:晓风残月Lx
 * @Date: 2022/11/19 10:58
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientDao, Patient> implements PatientService {

    @Resource
    private PatientDao patientDao;


    @Resource
    private PictureService pictureService;

    @Override
    public R savePatient(Patient patient) {
        boolean save = save(patient);
        log.debug(String.valueOf(save));
        if (!save) {
            return R.error("病人信息添加失败！");
        }
        return R.success(patient,"病人信息添加成功！");
    }

    /*
    * 插入病人信息返回pId
    * */
    @Override
    public Long insertPatient(Patient patient) {
        BaseMapper<Patient> mapper = this.getBaseMapper();
        Long insert = Long.valueOf(mapper.insert(patient));
        if (insert > 0)
        {
            return Long.valueOf(patient.getPId());
        }
            return 0L;
    }

    @Override
    public Page<Patient> getList(Page<Patient> patientPage) {
        return patientDao.selectPage(patientPage, null);
    }

    @Override
    public Page<Patient> getListByCreateId(Page<Patient> patientPage, Long id) {
        QueryWrapper<Patient> patientQueryWrapper = new QueryWrapper<>();
        patientQueryWrapper.eq("create_by",id);
        return patientDao.selectPage(patientPage,patientQueryWrapper);
    }

    @Override
    public Page<Patient> getListBySearchName(Page<Patient> patientPage, String name) {
        return patientDao.selectPage(patientPage, Wrappers.<Patient>lambdaQuery().like(Patient::getName,name));
    }

    @Override
    public Page<Patient> getListBySearchNameAndCreateId(Page<Patient> patientPage, String name, Long id) {
        QueryWrapper<Patient> patientQueryWrapper = new QueryWrapper<>();
        patientQueryWrapper.like("name",name).eq("create_by",id);
        return patientDao.selectPage(patientPage, patientQueryWrapper);
    }

    @Override
    public boolean deleteById(Integer id) {
        pictureService.deleteByPId(id);
        return this.removeById(id);
    }

    @Override
    public Patient getPatient(Integer pId) {
        return patientDao.selectOne(Wrappers.<Patient>lambdaQuery().eq(Patient::getPId,pId));
    }

    @Override
    public Patient getOneByPId(Integer pId) {
        return patientDao.selectById(pId);
    }

    @Override
    public int updatePatient(Patient patient) {
        return  patientDao.updateById(patient);

    }

}
